package com.tuanzi.loan.business.repository.receipt.statistics.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.StatLoanReceiptOrder.STAT_LOAN_RECEIPT_ORDER;

import java.math.BigDecimal;

import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.repository.receipt.statistics.custom.ReceiptOrderStatisticsRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.receipt.statistics.ReceiptOrderStatisticsRequest;
import com.tuanzi.loan.vo.response.receipt.statistics.ReceiptOrderStatisticsResponse;

/**
 * @author Ray 2017/09/21
 */
@Repository
public class ReceiptOrderStatisticsRepositoryImpl extends JOOQBaseRepositoryPlus implements ReceiptOrderStatisticsRepositoryCustom {

	@Override
	protected TableImpl getTable() {
		return STAT_LOAN_RECEIPT_ORDER;
	}

	public Page<ReceiptOrderStatisticsResponse> list(ReceiptOrderStatisticsRequest searchRequest,Pageable pageable){
		//如果产品选为全部或者来源选择全部或者来源为"甲方外推"的时候，要使用统计函数
		if (searchRequest.getReceiptProductId()==null||searchRequest.getSourceFrom()==null||searchRequest.getSourceFrom()==SourceFromType.JIA_FANG_EXTEND.getSourceFromId()) {
			fields(STAT_LOAN_RECEIPT_ORDER.DATE, STAT_LOAN_RECEIPT_ORDER.PRODUCT_NAME,STAT_LOAN_RECEIPT_ORDER.SOURCE_FROM,
					DSL.sum(STAT_LOAN_RECEIPT_ORDER.ORDER_COUNT).as(STAT_LOAN_RECEIPT_ORDER.ORDER_COUNT),
					DSL.sum(STAT_LOAN_RECEIPT_ORDER.WAITING_COUNT).as(STAT_LOAN_RECEIPT_ORDER.WAITING_COUNT),
					DSL.sum(STAT_LOAN_RECEIPT_ORDER.APPROVING_COUNT).as(STAT_LOAN_RECEIPT_ORDER.APPROVING_COUNT),
					DSL.sum(STAT_LOAN_RECEIPT_ORDER.APPROVE_PASS_COUNT).as(STAT_LOAN_RECEIPT_ORDER.APPROVE_PASS_COUNT),
					DSL.sum(STAT_LOAN_RECEIPT_ORDER.REJECT_COUNT).as(STAT_LOAN_RECEIPT_ORDER.REJECT_COUNT),
					DSL.sum(STAT_LOAN_RECEIPT_ORDER.LOANED_COUNT).as(STAT_LOAN_RECEIPT_ORDER.LOANED_COUNT),
			        DSL.sum(STAT_LOAN_RECEIPT_ORDER.CANCEL_COUNT).as(STAT_LOAN_RECEIPT_ORDER.CANCEL_COUNT),
					DSL.sum(STAT_LOAN_RECEIPT_ORDER.SETTLE_AMOUNT).as(STAT_LOAN_RECEIPT_ORDER.SETTLE_AMOUNT));
		}
		eq(STAT_LOAN_RECEIPT_ORDER.PRODUCT_ID, searchRequest.getReceiptProductId());
		eq(STAT_LOAN_RECEIPT_ORDER.SOURCE_FROM, searchRequest.getSourceFrom());
        ge(STAT_LOAN_RECEIPT_ORDER.DATE, searchRequest.getStartDate());
		le(STAT_LOAN_RECEIPT_ORDER.DATE, searchRequest.getEndDate());
		groupBy(STAT_LOAN_RECEIPT_ORDER.DATE);
		orderBy(STAT_LOAN_RECEIPT_ORDER.DATE, Sort.Direction.DESC);
		Page<ReceiptOrderStatisticsResponse> receiptOrderStatisticsResponses = search(pageable, new ReceiptOrderStatisticsResponseMapper());
		if (searchRequest.getReceiptProductId()==null) {
			receiptOrderStatisticsResponses.forEach(x->x.setProductName("全部"));
		}
		if (searchRequest.getSourceFrom()==null) {
			receiptOrderStatisticsResponses.forEach(x->x.setSourceFromName("全部"));
		}
		return receiptOrderStatisticsResponses;
	}

	private final class ReceiptOrderStatisticsResponseMapper implements RecordMapper<Record, ReceiptOrderStatisticsResponse> {

		@SuppressWarnings("Duplicates")
		@Override
		public ReceiptOrderStatisticsResponse map(Record record) {
			ReceiptOrderStatisticsResponse response = new ReceiptOrderStatisticsResponse();
			response.setDate(record.get(STAT_LOAN_RECEIPT_ORDER.DATE));
			response.setApprovingCount(record.get(STAT_LOAN_RECEIPT_ORDER.APPROVING_COUNT, BigDecimal.class).longValue());
            response.setApprovePassCount(record.get(STAT_LOAN_RECEIPT_ORDER.APPROVE_PASS_COUNT,BigDecimal.class).longValue());
			response.setOrderCount(record.get(STAT_LOAN_RECEIPT_ORDER.ORDER_COUNT, BigDecimal.class).longValue());
			response.setLoanedCount(record.get(STAT_LOAN_RECEIPT_ORDER.LOANED_COUNT, BigDecimal.class).longValue());
			response.setRejectCount(record.get(STAT_LOAN_RECEIPT_ORDER.REJECT_COUNT, BigDecimal.class).longValue());
			response.setWaitingCount(record.get(STAT_LOAN_RECEIPT_ORDER.WAITING_COUNT, BigDecimal.class).longValue());
			response.setCancelCount(record.get(STAT_LOAN_RECEIPT_ORDER.CANCEL_COUNT, BigDecimal.class).longValue());
			response.setProductName(record.get(STAT_LOAN_RECEIPT_ORDER.PRODUCT_NAME));
			response.setSourceFromName(SourceFromType.get(record.get(STAT_LOAN_RECEIPT_ORDER.SOURCE_FROM)));
			BigDecimal settleAmount = record.get(STAT_LOAN_RECEIPT_ORDER.SETTLE_AMOUNT,BigDecimal.class);
			response.setSettleAmount(settleAmount == null ? null : settleAmount.longValue());
			return response;
		}
	}

	public enum SourceFromType {
		JIE_TIAO_AREA(1, "借条专区"),
		DAI_KUAN_LIST(2, "贷款列表"),
		JIA_FANG_EXTEND(3, "甲方外推"),
		ONE_KEY_SUBMIT(4, "一键投递"),
		ZHENG_XIN_BASE(5, "基础版征信"),
		ZHENG_XIN_ADVANCE(6, "高级版征信");

		private String sourceFromName;
		private Integer sourceFromId;

		SourceFromType(Integer sourceFromId, String sourcefromName) {
			this.sourceFromId = sourceFromId;
			this.sourceFromName = sourcefromName;
		}

		public static String get(Integer sourceFromId) {
			for (SourceFromType sourceFromType : SourceFromType.values()) {
				if (sourceFromType.sourceFromId == sourceFromId) {
					return sourceFromType.sourceFromName;
				}
			}
			return "";
		}

		public String getSourceFromName() {
			return sourceFromName;
		}

		public Integer getSourceFromId() {
			return sourceFromId;
		}
	}

}
